package com.beiming.libra.service.impl;

import com.beiming.libra.common.enums.CaseUserTypeEnum;
import com.beiming.libra.common.enums.ProofStatusEnum;
import com.beiming.libra.common.utils.BeanConvertUtils;
import com.beiming.libra.common.utils.CommonsUtils;
import com.beiming.libra.dao.ArbitrationCaseDocumentMapper;
import com.beiming.libra.dao.ArbitrationCaseProofMapper;
import com.beiming.libra.domain.ArbitrationCaseProof;
import com.beiming.libra.domain.ArbitrationCaseProofFile;
import com.beiming.libra.domain.dto.requestdto.ArbitrationCaseProofCheckRequestDTO;
import com.beiming.libra.domain.dto.requestdto.ArbitrationCaseProofFileRequestDTO;
import com.beiming.libra.domain.dto.requestdto.ArbitrationCaseProofRequestDTO;
import com.beiming.libra.domain.dto.responsedto.CaseProofResponseDTO;
import com.beiming.libra.service.ArbitrationCaseProofFileService;
import com.beiming.libra.service.ArbitrationCaseProofService;
import com.beiming.libra.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zhengyu
 * @title
 * @create_time 2018/6/1
 */
@Slf4j
@Service
public class ArbitrationCaseProofServiceImpl implements ArbitrationCaseProofService {

  @Autowired
  private ArbitrationCaseProofMapper arbitrationCaseProofMapper;
  @Autowired
  private ArbitrationCaseDocumentMapper arbitrationCaseDocumentMapper;
  @Autowired
  private FileService fileService;
  @Autowired
  private ArbitrationCaseProofFileService arbitrationCaseProofFileService;

  /**
   * 获取举证质证服务
   *
   * @param caseId 案件申请书id
   */
  @Override
  public List<CaseProofResponseDTO> listArbitrationCaseProofs(String caseId, String status) {
    List<ArbitrationCaseProof> arbitrationCaseProofs = new ArrayList<ArbitrationCaseProof>();
    Map<String, Object> param = new HashMap<>();
    param.put("caseId", caseId);
    if ("5".equals(status)) {
      param.put("deltatus", status);
    } else {
      param.put("status", status);
    }
    arbitrationCaseProofs = arbitrationCaseProofMapper.getArbitrationCaseProofList(param);

    //拼装对应证据信息
    List<CaseProofResponseDTO> arbitrationCaseProofDTOs = new ArrayList<CaseProofResponseDTO>();
    for (ArbitrationCaseProof po:arbitrationCaseProofs) {
      List<ArbitrationCaseProofFile> arbitrationCaseProofFiles = arbitrationCaseProofFileService.listArbitrationCaseProofFiles(
          po.getId());

      CaseProofResponseDTO caseProofResponseDTO = BeanConvertUtils.copyBean(po, CaseProofResponseDTO.class);
      caseProofResponseDTO.setArbitrationCaseProofFiles(arbitrationCaseProofFiles);
      arbitrationCaseProofDTOs.add(caseProofResponseDTO);
    }

    return arbitrationCaseProofDTOs;
  }

  /**
   * 精确获取举证质证服务
   *
   * @param id id
   */
  @Override
  public ArbitrationCaseProof getArbitrationCaseProof(String id) {
    return arbitrationCaseProofMapper.selectByPrimaryKey(id);
  }

  /**
   * 批量保存更新举证质证信息服务
   *
   * @param arbitrationCaseProofRequestDTO 待保存的举证质证信息及对应附件信息
   */
  @Override
  public void createArbitrationCaseProof(ArbitrationCaseProofRequestDTO arbitrationCaseProofRequestDTO) {
    ArbitrationCaseProof arbitrationCaseProof = new ArbitrationCaseProof();
    arbitrationCaseProof.setId(CommonsUtils.get32BitUUID());
    arbitrationCaseProof.setCaseId(arbitrationCaseProofRequestDTO.getCaseId());
    arbitrationCaseProof.setProofPersonId(arbitrationCaseProofRequestDTO.getUserId());
    arbitrationCaseProof.setUserType(arbitrationCaseProofRequestDTO.getUserType());
    arbitrationCaseProof.setUserTypeName(getUserTypeName(arbitrationCaseProof.getUserType()));

    //保存举证质证附件关联信息表
    for (ArbitrationCaseProofFileRequestDTO arbitrationCaseProofFileRequestDTO :arbitrationCaseProofRequestDTO.getArbitrationCaseProofFileRequestDTOs()) {
      ArbitrationCaseProofFile arbitrationCaseProofFile = BeanConvertUtils.copyBean(arbitrationCaseProofFileRequestDTO, ArbitrationCaseProofFile.class);
      arbitrationCaseProofFile.setProofId(arbitrationCaseProof.getId());
      arbitrationCaseProofFileService.saveArbitrationCaseProofFile(arbitrationCaseProofFile);
    }

    //保存举证质证信息
    arbitrationCaseProofMapper.insert(arbitrationCaseProof);
  }

  /**
   * 批量创建待审核举证质证信息
   *
   * @param arbitrationCaseProofRequestDTO 举证质证及相关文件信息
   */
  @Override public void createPreArbitrationCaseProof(
      ArbitrationCaseProofRequestDTO arbitrationCaseProofRequestDTO) {
    ArbitrationCaseProof arbitrationCaseProof = new ArbitrationCaseProof();
    arbitrationCaseProof.setId(CommonsUtils.get32BitUUID());
    arbitrationCaseProof.setCaseId(arbitrationCaseProofRequestDTO.getCaseId());
    arbitrationCaseProof.setProofPersonId(arbitrationCaseProofRequestDTO.getUserId());
    arbitrationCaseProof.setUserType(arbitrationCaseProofRequestDTO.getUserType());
    arbitrationCaseProof.setUserTypeName(getUserTypeName(arbitrationCaseProof.getUserType()));
    arbitrationCaseProof.setStatus(ProofStatusEnum.UNCHECKED.getCode());

    //保存举证质证附件关联信息表
    for (ArbitrationCaseProofFileRequestDTO arbitrationCaseProofFileRequestDTO :arbitrationCaseProofRequestDTO.getArbitrationCaseProofFileRequestDTOs()) {
      ArbitrationCaseProofFile arbitrationCaseProofFile = BeanConvertUtils.copyBean(arbitrationCaseProofFileRequestDTO, ArbitrationCaseProofFile.class);
      arbitrationCaseProofFile.setProofId(arbitrationCaseProof.getId());
      arbitrationCaseProofFileService.saveArbitrationCaseProofFile(arbitrationCaseProofFile);
    }

    //保存举证质证信息
    arbitrationCaseProofMapper.insert(arbitrationCaseProof);
  }

  /**
   * 批量更新举证质证信息
   *
   * @param arbitrationCaseProofCheckRequestDTO 举证质证及相关文件信息
   */
  @Override public void updatgeArbitrationCaseProof(
    ArbitrationCaseProofCheckRequestDTO arbitrationCaseProofCheckRequestDTO) {
    ArbitrationCaseProof arbitrationCaseProof = arbitrationCaseProofMapper.selectByPrimaryKey(
        arbitrationCaseProofCheckRequestDTO.getId());
    arbitrationCaseProof.setLegality(arbitrationCaseProofCheckRequestDTO.getLegality());
    arbitrationCaseProof.setRealness(arbitrationCaseProofCheckRequestDTO.getRealness());
    arbitrationCaseProof.setRelation(arbitrationCaseProofCheckRequestDTO.getRelation());
    arbitrationCaseProof.setReason(arbitrationCaseProofCheckRequestDTO.getReason());

    arbitrationCaseProofMapper.updateByPrimaryKey(arbitrationCaseProof);
  }

  /**
   * 举证质证审核
   *
   * @param id     举证质证表id
   * @param status 审核状态
   */
  @Override public void updateStatus(String id, String status) {
    ArbitrationCaseProof arbitrationCaseProof = arbitrationCaseProofMapper.selectByPrimaryKey(id);
    arbitrationCaseProof.setStatus(status);
    arbitrationCaseProofMapper.updateByPrimaryKey(arbitrationCaseProof);
  }

  public String getUserTypeName(String userTypeID) {
    if (userTypeID.equals(CaseUserTypeEnum.AGENT_APPLICANT.getCode())) {
      return CaseUserTypeEnum.AGENT_APPLICANT.getName();
    } else if (userTypeID.equals(CaseUserTypeEnum.APPLICANT.getCode())) {
      return CaseUserTypeEnum.APPLICANT.getName();
    } else if (userTypeID.equals(CaseUserTypeEnum.RESPONDENT.getCode())) {
      return CaseUserTypeEnum.RESPONDENT.getName();
    } else if (userTypeID.equals(CaseUserTypeEnum.AGENT_RESPONDENT.getCode())) {
      return CaseUserTypeEnum.AGENT_RESPONDENT.getName();
    } else {
      return  null;
    }
  };

}
